package prassein.rsm.web;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.beans.factory.annotation.Autowired;

import prassein.rsm.common.utils.Constants;
import prassein.rsm.entity.base.Resume;
import prassein.rsm.entity.base.ResumeFile;
import prassein.rsm.service.base.ResumeFileManager;
import prassein.rsm.service.base.ResumeManager;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Results({ @Result(name = "success", location = "reloadReviewResume.jsp") })
public class FileUploadAction extends ActionSupport implements
		ServletContextAware {

	private static final long serialVersionUID = -5016873153441103539L;

	private File doc;
	private String fileName;
	private String contentType;

	private ServletContext context;

	@Autowired
	private ResumeManager resumeManager;
	
	@Autowired
	private ResumeFileManager resumeFileManager;
	
	public void setDoc(File file) {
		this.doc = file;
	}

	public void setDocFileName(String fileName) {
		this.fileName = fileName;
	}

	public void setDocContentType(String contentType) {
		this.contentType = contentType;
	}

	public void setServletContext(ServletContext context) {
		this.context = context;
	}

	public String execute() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		try {
			if (session.get(Constants.LATEST_EDIT_REUMEID) == null) {
				HttpServletResponse response = ServletActionContext.getResponse();
				response.getWriter().write("alert(请先创建简历)");
				session.put(Constants.SUBMIT_RESULE, "2");
				return ERROR;
			} else {
				String targetDirectory = context.getRealPath("/upload");
				String targetFileName = generateFileName(fileName);
				targetDirectory = targetDirectory + "/" + targetFileName;
				File target = new File(targetDirectory, fileName);
		
				FileUtils.copyFile(doc, target);
				
				ResumeFile resumeFile = new ResumeFile();;
				resumeFile.setFileName(target.getName());
				resumeFile.setLocation("/" + targetFileName + "/" + target.getName());
				resumeFileManager.save(resumeFile);
				
				int id = Integer.parseInt(session.get(Constants.LATEST_EDIT_REUMEID).toString());
				Resume resume = resumeManager.getResume(id);
				if (resume.getResumeFile1() == null || resume.getResumeFile1() == -1) {
					resume.setResumeFile1(resumeFile.getId());
				} else if (resume.getResumeFile2() == null || resume.getResumeFile2() == -1) {
					resume.setResumeFile2(resumeFile.getId());
				} else if (resume.getResumeFile3() == null || resume.getResumeFile3() == -1) {
					resume.setResumeFile3(resumeFile.getId());
				} else {
					resume.setResumeFile4(resumeFile.getId());
				}
				
				resumeManager.save(resume);
				session.put(Constants.SUBMIT_RESULE, "1");
				return SUCCESS;
			}
		} catch (Exception e) {
			session.put(Constants.SUBMIT_RESULE, "2");
			return ERROR;
		}
	}

	private String generateFileName(String fileName) {
		DateFormat format = new SimpleDateFormat("yyMMddHHmmss");
		String formatDate = format.format(new Date());

		int random = new Random().nextInt(10000);

//		int position = fileName.lastIndexOf(".");
//		String extension = fileName.substring(position);

		return formatDate + random/* + extension*/;
	}

	public ResumeManager getResumeManager() {
		return resumeManager;
	}

	public void setResumeManager(ResumeManager resumeManager) {
		this.resumeManager = resumeManager;
	}
	public ResumeFileManager getResumeFileManager() {
		return resumeFileManager;
	}

	public void setResumeFileManager(ResumeFileManager resumeFileManager) {
		this.resumeFileManager = resumeFileManager;
	}

}
